<template>
    <div class="views-hugong-updtself">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加员工 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item
                        label="工号"
                        prop="gonghao"
                        required
                        :rules="[{required:true, message:'请填写工号'}, {validator:rule.checkRemote, message:'内容重复了', checktype:'update', module:'hugong', col:'gonghao', id:form.id, trigger:'blur'}]"
                    >
                        <el-input type="text" placeholder="输入工号" style="width: 450px" v-model="form.gonghao" />
                    </el-form-item>

                    <el-form-item label="护工姓名" prop="hugongxingming">
                        <el-input type="text" placeholder="输入护工姓名" style="width: 450px" v-model="form.hugongxingming" />
                    </el-form-item>

                    <el-form-item label="性别" prop="xingbie">
                        <el-select v-model="form.xingbie"
                            ><el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="年龄" prop="nianling" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]">
                        <el-input type="number" placeholder="输入年龄" style="width: 450px" v-model.number="form.nianling" />
                    </el-form-item>

                    <el-form-item label="籍贯" prop="jiguan"> <el-input type="text" placeholder="输入籍贯" style="width: 450px" v-model="form.jiguan" /> </el-form-item>

                    <el-form-item label="护工电话" prop="hugongdianhua" :rules="[{validator:rule.checkPhone, message:'请输入正确手机号码'}]">
                        <el-input type="text" placeholder="输入护工电话" style="width: 450px" v-model="form.hugongdianhua" />
                    </el-form-item>

                    <el-form-item label="身份证" prop="shenfenzheng" :rules="[{validator:rule.checkCard, message:'请输入有效身份证号码'}]">
                        <el-input type="text" placeholder="输入身份证" style="width: 450px" v-model="form.shenfenzheng" />
                    </el-form-item>

                    <el-form-item label="头像" prop="touxiang"> <e-upload-image v-model="form.touxiang" is-paste></e-upload-image> </el-form-item>

                    <el-form-item label="在职状态" prop="zaizhizhuangtai">
                        <el-select v-model="form.zaizhizhuangtai"
                            ><el-option label="在职" value="在职"></el-option>
                            <el-option label="离职" value="离职"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="简介" prop="jianjie"> <el-input type="textarea" v-model="form.jianjie"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useHugongFindById, canHugongFindById, canHugongUpdate } from "@/module";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const form = useHugongFindById(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);

    const submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canHugongUpdate(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("更新成功");
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };
</script>

<style scoped lang="scss">
    .views-hugong-updtself {
    }
</style>
